package com.bjb.vr.operation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.ResCusContentAuthorityAo;
import com.bjb.vr.common.dto.ResContent;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.ResBundleService;
import com.bjb.vr.common.service.ResCusContentAuthorityService;
import com.bjb.vr.common.vo.ResBundleVo;
import com.bjb.vr.common.vo.ResCusContentAuthorityVo;
import com.bjb.vr.operation.service.ResDeviceCtrlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0.0
 * @description:客户内容授权管理
 * @author: caihaibo
 * @createDate: 2022/8/4
 */
@Slf4j
@RestController
@RequestMapping("/api/operation/v1")
public class ResCusContentAuthorityController {

    @Autowired
    ResCusContentAuthorityService resCusContentAuthorityService;

    @Autowired
    ResDeviceCtrlService resDeviceCtrlService;

    @Resource
    private ResBundleService resBundleService;
    /**
     * 资源授权管理-查询客户内容授权列表
     * @param resCusContentAuthorityAo
     * @return
     */
    @PostMapping("/queryResCusContAuthorityPage")
    public BaseResult<IPage<ResCusContentAuthorityVo>> queryResCusContAuthorityPage(@RequestBody ResCusContentAuthorityAo resCusContentAuthorityAo){
        IPage<ResCusContentAuthorityVo> resCusContentAuthorityIPage = resCusContentAuthorityService.queryResCusContAuthorityPage(resCusContentAuthorityAo);
        return CommonResult.Ok().setResult(resCusContentAuthorityIPage);
    }

    /**
     * 增加客户内容授权
     * @param resCusContentAuthorityAo
     * @return
     */
    @PostMapping("/addResCusContentAuthority")
    public BaseResult addResCusContentAuthority(@RequestBody ResCusContentAuthorityAo resCusContentAuthorityAo){
        Assert.notNull(resCusContentAuthorityAo.getTenantId(),"租户id不能为空");
        Assert.notNull(resCusContentAuthorityAo.getContentIds(),"资源id不能为空");
        Assert.notNull(resCusContentAuthorityAo.getUdids(),"设备udid不能为空");
        boolean success = resCusContentAuthorityService.addResCusContentAuthority(resCusContentAuthorityAo);
        if(!success){
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        resDeviceCtrlService.sendRestartCommand(resCusContentAuthorityAo.getTenantId(),resCusContentAuthorityAo.getUdids());
        return CommonResult.Ok();
    }

    /**
     * 修改客户内容授权
     * @param resCusContentAuthorityAo
     * @return
     */
    @PostMapping("/updateResCusContentAuthority")
    public BaseResult updateResCusContentAuthority(@RequestBody ResCusContentAuthorityAo resCusContentAuthorityAo){
        Assert.notNull(resCusContentAuthorityAo.getTenantId(),"租户id不能为空");
        Assert.notNull(resCusContentAuthorityAo.getContentIds(),"资源id不能为空");
        Assert.notNull(resCusContentAuthorityAo.getUdids(),"设备udid不能为空");
        boolean success = resCusContentAuthorityService.updateResCusContentAuthority(resCusContentAuthorityAo);
        if(!success){
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        resDeviceCtrlService.sendRestartCommand(resCusContentAuthorityAo.getTenantId(),resCusContentAuthorityAo.getUdids());
        return CommonResult.Ok();
    }


    /**
     * 删除客户内容授权
     * @param resCusContentAuthorityAo
     * @return
     */
    @PostMapping("/deleteResCusContentAuthority")
    public BaseResult deleteResCusContentAuthority(@RequestBody ResCusContentAuthorityAo resCusContentAuthorityAo){
        boolean success = resCusContentAuthorityService.deleteResCusContentAuthority(resCusContentAuthorityAo);
        if(!success){
            return CommonResult.Fail(BaseErrorCode.ERROR_REMOVE);
        }
        return CommonResult.Ok();
    }


    /**
     * 查询内容授权详情
     * @param resCusContentAuthorityAo
     * @return
     */
    @PostMapping("/queryResCusContentAuthorityDetail")
    public BaseResult<ResCusContentAuthorityVo> queryResCusContentAuthorityDetail(@RequestBody ResCusContentAuthorityAo resCusContentAuthorityAo){
        ResCusContentAuthorityVo resCusContentAuthorityVo = resCusContentAuthorityService.queryResCusContentAuthorityDetail(resCusContentAuthorityAo);
        return CommonResult.Ok().setResult(resCusContentAuthorityVo);
    }


    /**
     * 资源授权管理-获取产品包列表
     *
     * @return
     */
    @PostMapping("/queryResourceAuthorityBundleList")
    public BaseResult<IPage<ResBundleVo>> queryResourceAuthorityBundleList(@RequestBody ResCusContentAuthorityAo resCusContentAuthorityAo) {
        IPage<ResBundleVo> resBundleVos = resBundleService.queryResourceAuthorityBundleList(resCusContentAuthorityAo);
        return CommonResult.Ok().setResult(resBundleVos);
    }

    /**
     * 资源授权管理-获取多个资源包资源列表
     *
     * @param resCusContentAuthorityAo
     * @return
     * @throws Exception
     */
    @PostMapping("/queryBundleResourceList")
    public BaseResult<List<ResContent>> queryAuthorityBundleResourceList(@RequestBody ResCusContentAuthorityAo resCusContentAuthorityAo) {
        Assert.notNull(resCusContentAuthorityAo.getResBundleIds(), "资源包ID不能为空");
        List<ResContent> resBundleDetail = resBundleService.queryAuthorityBundleResourceList(resCusContentAuthorityAo);
        return CommonResult.Ok().setResult(resBundleDetail);
    }
}
